#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool arr[10];
inline bool check(int a) {
	while (a) {
		if (arr[a%10]) return false;
		a=a/10;
	}
	return true;
}
int main() {
	int n;
	scanf("%d", &n);
	int l,k;
	while (n--) {
		memset(arr, 0, sizeof(arr));
		scanf("%d%d", &l, &k);
		int t=l;
		while (t) arr[t%10]=true, t/=10;
		t=k;
		while (t) arr[t%10]=true, t/=10;
		int res=0;
		for (int i=1; i<=1000; ++i) {
			if (i%l==0) continue;
			if (i%k==0) continue;
			if (check(i)==false) continue;
			++res;
		}
		printf("%d\n", res);
	}
	return 0;
}
